class Solution
{
public:
    int longestConsecutive(vector<int>& nums)
    {
        unordered_set<int> st(nums.begin(), nums.end());
        int ans = 0;
        for (int x : st)
        {
            if (st.contains(x - 1))
            {
                continue;
            }
            int y = x + 1;
            while (st.contains(y))
            {
                y++;
            }
            ans = max(ans, y - x);
            if (ans * 2 >= st.size())
                break;
        }
        return ans;
    }
};